package leetcode;

/**
 * Created by Ghost on 2014/12/6.
 */
public class Find_Peak_Element {
    public int findPeakElement(int[] num) {
        if (num.length==1){
            return 0;
        }
        if(num[0]>num[1])
            return 0;
        if(num[num.length-1]>num[num.length-2])
            return num.length-1;

        int left = 1,right = num.length-2;
        while(left<=right){
            int mid = (left+right)>>1;
            if(num[mid]>num[mid-1] && num[mid]>num[mid+1]){
                return mid;
            }
            else if(num[mid]>num[mid-1]){
                left = mid+1;
            }
            else{
                right = mid-1;
            }
        }
        return -1;
    }

    public static void main(String[] args){
        Find_Peak_Element find_peak_element = new Find_Peak_Element();
        int[] num = {1,2,3,7,4,3,1};
        System.out.println(find_peak_element.findPeakElement(num));
    }
}
